package oop.ex3.crosswords;

import java.util.Comparator;

/**
 * An implementation of Comparator, <br>
 * compare Crosswords entries according to ex3. description<br>
 * <Br>
 * 
 * <b>Ordering of boards</b><br>
 * Use the following arrangement for next moves:<br>
 * 1) First try to place longer words into the crossword, and then shorter ones (words with the same size should be
 * sorted alphabetically in ascending order (i.e. a < b < ... < z))<br>
 * 2) For each word, first select entries that have more overlapping (already filled crossword cells) letters with
 * the given word and then select entries with less overlapping letters.<br>
 * 3) For the same word and entries with same number of overlapping letters, sort entries as follows:<br>
 * a. X coordinate [meaning that (this > other) iff (this.x > other.x)]<br>
 * b. Y coordinate [meaning that (this > other) iff (this.y > other.y)]<br>
 * c. vertical/horizontal [meaning that (this > other) iff this is vertical and other is horizontal]<br>
 * 
 * @author kobi_atiya
 * @author tsachiho
 * 
 * @param <S>
 */
public class MyCrosswordEntryComparator<S> implements Comparator<CrosswordEntry> {

    @Override
    public int compare(CrosswordEntry o1, CrosswordEntry o2) {
        if (!(o1 instanceof MyCrosswordEntry) || !(o2 instanceof MyCrosswordEntry)) return 0;

        if (o1.getLength() != o2.getLength()) return Integer.signum(o2.getLength() - o1.getLength());

        if (o1.getTerm().compareTo(o2.getTerm()) != 0) return o1.getTerm().compareTo(o2.getTerm());

        int overlappingDiff = ((MyCrosswordEntry) o2).getOverlapping() - ((MyCrosswordEntry) o1).getOverlapping();

        if (overlappingDiff != 0) return Integer.signum(overlappingDiff);

        MyCrosswordPosition o1pos = (MyCrosswordPosition) o1.getPosition();
        MyCrosswordPosition o2pos = (MyCrosswordPosition) o2.getPosition();
        return o1pos.compareTo(o2pos);

    }
}
